﻿using System;
using System.IO;

namespace MD.ImageProcessing.Cell
{
    public class Singleton<T> where T : class, new()
    {
        private Singleton() { }

        private static readonly Lazy<T> instance = new Lazy<T>(() => new T());

        public static T Instance { get { return instance.Value; } }
    }

    internal class CellSaver
    {
        public string SavePath { get; set; }

        public void Save(CellState cell)
        {
            SavePath = "D:\\temp\\CellImages\\" + cell.CellId.ToString();

            if (!Directory.Exists(SavePath))
            {
                Directory.CreateDirectory(SavePath);
            }

            var path = Path.Combine(SavePath, "1.jpg");
            cell.State.Image.ToManagedImage().Save(path);
        }

        public void Save(Cell cell)
        {
            int i = 0;
            if (cell.States.Count % 10 == 0)
            {
                foreach (var cellState in cell.States)
                {
                    SavePath = "D:\\temp\\CellImages\\" + cell.Id.ToString();

                    if (!Directory.Exists(SavePath))
                    {
                        Directory.CreateDirectory(SavePath);
                    }

                    var path = Path.Combine(SavePath, i + ".jpg");
                    i++;
                    try
                    {
                        cellState.State.Image.ToManagedImage().Save(path);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
    }
}